package com.bw.util;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zhenzi.sms.ZhenziSmsClient;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送短信
 */
public class ZzyUtils {
    public static final String APP_ID = "113344";
    public static final String APP_SECRET = "d7e1bbb9-3f4c-45fa-963a-79ed6c2d859a";
    public static final String API_URL = "https://sms_developer.zhenzikj.com";
    public static final String TEMPLATE_ID = "11942";


    /**
     * 发送短信
     *
     * @param phone      电话号码
     * @param randomCode 4位随机验证码
     * @param minute     有效时间,单位分钟
     * @return 发送结果
     */
    public static boolean sendSms(String phone, String randomCode, Integer minute) {
        try {
            ZhenziSmsClient client = new ZhenziSmsClient(API_URL, APP_ID, APP_SECRET);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("number", phone);//电话号码
            params.put("templateId", TEMPLATE_ID);//短信模板
            String[] templateParams = new String[2];
            templateParams[0] = randomCode;
            templateParams[1] = minute + "";
            params.put("templateParams", templateParams);
            String result = client.send(params);//发送短信
            JSONObject jsonObject = JSONUtil.parseObj(result);
            Object code = jsonObject.get("code");
            if ("0".equals(code + "")) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) throws Exception {
        String code = RandomUtil.randomNumbers(4);
        boolean b = sendSms("17611221735", code,5);
        System.out.println("发送内容：" + code + ",发送结果：" + b);


    }
}

